package com.wrench.elevator.tida.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;

public class JPushUtil
{
    private static final String ANDROID_appKey = "c00d78674b2d9637dace0ab4";
    
    private static final String ANDROID_masterSecret = "57138ad6766fe76b8a6dd957";
    
    private static final String IOS_appKey = "19b4fde5118acbe9ae5f2a03";
    
    private static final String IOS_masterSecret = "b072a5cc17e4884da87bc41f";
    
    private static final String AndroidBanZi_appKey = "91d33d7ff9ecbdd6b6d311ab";
    
    private static final String AndroidBanZi_masterSecret = "a6762ed583e6c7cea4ee2ee8";
    
    private static final String AndroidJiaoXiang_appKey = "457ba3073832d90d1aed1f77";
    
    private static final String AndroidJiaoXiang_masterSecret = "7690f38b759b78cf163536ee";
    
    private static final String AndroidJiaoXiang_alias="13065ffa4e0c964de1d";
    
    public static boolean pushToAndroidBanZi(String face_pic_url, String user_id, String banzi_alias)
    {
        JPushClient client = new JPushClient(AndroidBanZi_masterSecret, AndroidBanZi_appKey);
        PushPayload payLoad =
            PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(banzi_alias))
                .setMessage(Message.content("{\"face_pic_url\":\"" + face_pic_url + "\",\"user_id\":\"" + user_id
                    + "\"}"))
                .build();
        int statusCode = -1;
        try
        {
            statusCode = client.sendPush(payLoad).statusCode;
        }
        catch (APIConnectionException | APIRequestException e)
        {
            
            e.printStackTrace();
        }
        return statusCode == 0;
    }
    
    /**
     * 推送给轿厢 1、用户新增：用户入驻该单元 2、用户删除：用户搬离该单元 3、用户锁定：用户在最后一次乘梯后余额不足 4、用户解锁：余额不足以后的充值
     * */
    public static boolean pushToJiaoxiang(Map<String, Object> contents, String alias)
    {
        
        JPushClient client = new JPushClient(AndroidJiaoXiang_masterSecret, AndroidJiaoXiang_appKey);
        PushPayload payLoad =
            PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setMessage(Message.content(JSON.toJSONString(contents)))
                .build();
        int statusCode = -1;
        try
        {
            statusCode = client.sendPush(payLoad).statusCode;
        }
        catch (APIConnectionException | APIRequestException e)
        {
            
            e.printStackTrace();
        }
        return statusCode == 0;
    }



    public static boolean pushToApp(Map<String, Object> contents, String alias)
    {

       return pushToAndroid(contents, alias)||pushToIOS(contents, alias);
    }


    public static boolean pushToAndroid(Map<String, Object> contents, String alias)
    {

        JPushClient client = new JPushClient(ANDROID_appKey, ANDROID_masterSecret);
        PushPayload payLoad =
                PushPayload.newBuilder()
                        .setPlatform(Platform.all())
                        .setAudience(Audience.alias(alias))
                        .setMessage(Message.content(JSON.toJSONString(contents)))
                        .build();
        int statusCode = -1;
        try
        {
            statusCode = client.sendPush(payLoad).statusCode;
        }
        catch (APIConnectionException | APIRequestException e)
        {

            e.printStackTrace();
        }
        return statusCode == 0;
    }



    public static boolean pushToIOS(Map<String, Object> contents, String alias)
    {

        JPushClient client = new JPushClient(IOS_appKey, IOS_masterSecret);
        PushPayload payLoad =
                PushPayload.newBuilder()
                        .setPlatform(Platform.all())
                        .setAudience(Audience.alias(alias))
                        .setMessage(Message.content(JSON.toJSONString(contents)))
                        .build();
        int statusCode = -1;
        try
        {
            statusCode = client.sendPush(payLoad).statusCode;
        }
        catch (APIConnectionException | APIRequestException e)
        {

            e.printStackTrace();
        }
        return statusCode == 0;
    }
    
    public static void main(String[] args)
    {
       
        pushToAndroidBanZi("a","b","170976fa8a8a27269a2");
       // pushToAndroidBanZi("upload/20171106190644.jpg", "0001201711031709131679091c5a880faf6fb5e6087eb1b2dc", "13065ffa4e0c964de1d");
    }
}
